<?php
/**
 *
 * User:xinhuazhang
 * Date:2024/7/18
 * TIPS:功能说明
 * Email:<1655626823@qq.com>
 */

namespace app\api\controller;

use app\common\controller\Frontend;

#[Apidoc\Title("我的赏金--->新版本2.0")]
class Myreward extends Frontend
{
    protected array $noNeedPermission = ['*'];

    /**
     * 我的赏金
     *
     */
    #[
        Apidoc\Title("我的赏金"),
        Apidoc\Tag("action income => '我的收款', expend => '我的付款'"),
        Apidoc\Method("POST"),
        Apdoc\version("2.0"),
        Apidoc\Url("/api/myreward/index"),
        Apidoc\Query(name: "action", type: "string", require: true, desc: "顶部tab", mock: "income"),
        Apidoc\Query(name: "page", type: "number", require: false, desc: "页码", mock: "1"),
        Apidoc\Returned("id", type: "int", desc: "Id 提现时候需要"),
        Apidoc\Returned("name", type: "string", desc: "标题（房源和找店名称）"),
        Apidoc\Returned("check_text", type: "string", desc: "我的收款状态 check_status  0 => '待提现',
        1 => '待审核',
        2 => '已支付',
        3 => '已驳回',"),
        Apidoc\Returned("pay_text", type: "string", desc: "我的付款状态 check_status等于2 => '已支付' 其它都是支付中"),
        Apidoc\Returned("order_amount", type: "number", desc: "悬赏佣金"),
        Apidoc\Returned("house_type", type: "string", desc: "houses房源，findstore 找房类型 对应的house_id"),

        Apidoc\Returned("truename", type: "string", desc: "收款人"),
        Apidoc\Returned("bankno", type: "string", desc: "银行卡号"),
        Apidoc\Returned("open_bank", type: "string", desc: "开户行"),
        Apidoc\Returned("check_msg", type: "string", desc: "审核信息"),
    ]
    public function index()
    {
        $action = $this->request->param('action', 'income');
        if ($action == 'expend') {
            $map = [
                ['pay_status', 'in', [1, 3]]
            ];
            $map[] = ['user_id', '=', $this->auth->id];
        } else {
            $map[] = ['to_uid', '=', $this->auth->id];
            $map[] = ['pay_status', '=', 3];
        }
        $hourseModel = new \app\admin\model\Houses();
        $findModel = new \app\common\model\Findstore();
        $statuArr = [
            0 => '未支付',
            1 => '付款成功',
            2 => '已退款',
            3 => '佣金已支付',
        ];
        $list = (new \app\common\model\Orders())
            ->where($map)
            ->order('id desc')
            ->field('id,order_amount,pay_status,to_uid,paid_time,check_status,check_msg,truename,bankno,open_bank,house_id,house_type,create_time,check_img')
            ->paginate()->each(function ($item) use ($hourseModel, $findModel, $action, $statuArr) {
                $name = '-';
                if ($item['house_type'] == 'houses') {
                    $name = $hourseModel->where(['id' => $item['house_id']])->value('name', '');
                } else {
                    $name = $findModel->where(['id' => $item['house_id']])->value('name', '');
                }
                //$name='这是标题标题标题标题' . $item['id'];
                $item['name'] = $name;
                $item['accept_user'] = [
                    'nickname' => '',
                    'mobile' => ''
                ];
                $item['domain_check_img']=$item['check_img']?request()->domain().$item['check_img']:'';
                $item['pay_text'] = '我的付款状态';
                $item['check_text'] = intval($item['check_status']);
                if ($action == 'expend') {
                    $item['pay_text'] = $item['check_status'] == 2 ? '已支付' : '支付中';
                    $item['accept_user'] = (new \app\admin\model\User())->where(['id' => $item['to_uid']])->field('nickname,mobile,avatar')->find();
                }
//                else {
//                    $item['pay_text'] = isset($statuArr[$item['pay_status']]) ? $statuArr[$item['pay_status']] : '-';
//                }
            });
        $this->success($action == 'expend' ? '我的付款列表' : '我的收款列表', [
            'lists' => $list,
            'map' => $map,
            'income' => $action,
        ]);
    }

    #[
        Apidoc\Title("提现提交"),
        Apidoc\tag("check_status=0提现，check_status=3重新提现"),
        Apidoc\Method("POST"),
        Apdoc\version("2.0"),
        Apidoc\Url("/api/myreward/withdraw"),
        Apidoc\Query(name: "id", type: "int", require: true, desc: "订单id", mock: "1"),
        Apidoc\Query("truename", type: "string", desc: "收款人"),
        Apidoc\Query("bankno", type: "string", desc: "银行卡号"),
        Apidoc\Query("open_bank", type: "string", desc: "开户行"),
        Apidoc\Param(name: "id", type: "int", require: true, desc: "订单id", mock: "1"),
        Apidoc\Param("truename", type: "string", desc: "收款人"),
        Apidoc\Param("bankno", type: "string", desc: "银行卡号"),
        Apidoc\Param("open_bank", type: "string", desc: "开户行"),
    ]
    public function withdraw()
    {
        $save_data = $this->request->param();
        $map = [
            ['id', '=', $this->request->param('id/d')],
            //['pay_status', 'in', [1, 2, 3]],
            ['to_uid', '=', $this->auth->id]
        ];
        $order = (new \app\common\model\Orders())
            ->where($map)->find();
        if (!$order) {
            $this->error($this->request->param('id/d').'暂无可提现订单'.$this->auth->id);
        }
        if (!in_array($order['check_status'], [0, 3])) {
            $this->error('订单状态不允许提现');
        }
        $save_data['check_status'] = 1;
        $save_data['check_msg'] = '';
        $save_data['check_img'] = '';
        $save_data['sumit_time'] = time();
        $save_data['update_time'] = time();
        $validate = \think\facade\Validate::rule('truename', 'require')
            ->rule(['bankno' => 'require','open_bank' => 'require'])
            ->message([
                'truename'=>'收款人不能为空',
                'bankno' => '银行卡号不能为空',
                'open_bank' => '开户行不能为空'
            ]);
        if (!$validate->check($save_data)) {
            $this->error($validate->getError());
        }
        $upinfo=$order
            ->allowField(['update_time', 'check_status', 'sumit_time', 'truename', 'bankno', 'open_bank','check_msg'])
            ->save($save_data);
        if(!$upinfo){
            $this->error('提现失败,请重试');
        }
        $this->success('信息提交成功，请耐心等待审核');
    }
}
